// pages/search/search.js
Page({
    data: {
      // 搜索条件
      searchType: 'name', // 默认按商品名搜索
      searchTypeStr:'商品名',
      searchTypeMap:[
          {key:'ip',value:'IP'},
          {key:'series',value:'系列'},
          {key:'product',value:'产品'},
          {key:'name',value:'商品名'},
      ], 
      searchKeyword: '',
      // 搜索结果
      goodsList: [],
      total: 0,
      page: 1,
      pageSize: 10,
      loading: false,
      hasMore: true,
      
      // 选择器显示状态
      showSearchTypePopup: false
    },
  
    onLoad: function(options) {
      // 接收从其他页面传递的搜索关键词
      if (options.keyword) {
        this.setData({
          searchKeyword: options.keyword
        });
        // 自动触发搜索
        this.onSearch();
      }
    },
  
    // 显示搜索类型选择器
    showSearchTypePicker() {
        console.log(1);
      this.setData({ showSearchTypePopup: true });
    },
  
    // 关闭搜索类型选择器
    onSearchTypePopupClose() {
      this.setData({ showSearchTypePopup: false });
    },
  
    // 搜索类型选择器确认事件
    onSearchTypePickerConfirm(event) {
        console.log(event);
        const {
            value,
            index
        } = event.detail;
    //   const { value } = event.detail;
      this.setData({
        searchType: value.key,
        searchTypeStr: value.value,
        showSearchTypePopup: false
      });
    },
  
    // 搜索关键词输入变化
    onSearchKeywordChange(e) {
      this.setData({
        searchKeyword: e.detail
      });
    },
  
    // 执行搜索
    onSearch() {
      if (!this.data.searchKeyword.trim()) {
        wx.showToast({
          title: '请输入搜索关键词',
          icon: 'none'
        });
        return;
      }
      
      // 重置搜索结果
      this.setData({
        page: 1,
        goodsList: [],
        total: 0,
        loading: true,
        hasMore: true
      });
      
      // 执行搜索
      this.searchGoods();
    },
   //重置
   onCancel(){
      // 重置搜索结果
      this.setData({
        searchKeyword:'',
        page: 1,
        goodsList: [],
        total: 0,
        loading: false,
        hasMore: true
      });
      
      // 执行搜索
    //   this.searchGoods(); 
   },

    // 搜索商品
    searchGoods() {
      const { searchType, searchKeyword, page, pageSize } = this.data;
      
      wx.cloud.callFunction({
        name: 'searchGoods',
        data: {
          searchType: searchType,
          searchKeyword: searchKeyword,
          page: page,
          pageSize: pageSize
        },
        success: (res) => {
            console.log(res);
          if (res.result && res.result.success) {
            res.result.data.forEach(item=>{
                let arr = item.goodsImage.split(',')
                if (arr.length) {
                    item.images = arr
                }
            })
            const newGoodsList = page === 1 
              ? res.result.data 
              : [...this.data.goodsList, ...res.result.data];
            
            const hasMore = newGoodsList.length < res.result.total;
            
            this.setData({
              goodsList: newGoodsList,
              total: res.result.total || 0,
              page: page + 1,
              loading: false,
              hasMore: hasMore
            });
          } else {
            wx.showToast({
              title: '搜索失败，请重试',
              icon: 'none'
            });
            this.setData({ loading: false });
          }
        },
        fail: (err) => {
          console.error('搜索商品失败', err);
          wx.showToast({
            title: '网络错误，请稍后再试',
            icon: 'none'
          });
          this.setData({ loading: false });
        }
      });
    },
  
    // 点击商品项
    onGoodsTap(e) {
        console.log(e);
      const goodsId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/goods/index?goodsId=${goodsId}`
    });
    },
  
    // 下拉刷新
    onPullDownRefresh() {
      if (this.data.loading) return;
      
      this.setData({
        page: 1,
        goodsList: [],
        total: 0,
        loading: true,
        hasMore: true
      });
      
      this.searchGoods().then(() => {
        wx.stopPullDownRefresh();
      });
    },
  
    // 上拉加载更多
    onReachBottom() {
      if (this.data.loading || !this.data.hasMore) return;
      
      this.setData({ loading: true });
      this.searchGoods();
    }
  });